package com.b2b2c.store.single.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.b2b2c.store.single.entity.StoreAuthGroup;
import com.b2b2c.store.single.entity.StoreAuthGroupAccess;
import com.b2b2c.store.single.entity.StoreAuthMenu;
import com.b2b2c.store.single.logic.StoreAuthGroupAccessLogic;
import com.b2b2c.store.single.logic.StoreAuthGroupLogic;
import com.b2b2c.store.single.logic.StoreAuthMenuLogic;
import com.b2b2c.store.single.service.IStoreAuthMenuService;
import com.b2b2c.store.single.service.IStoreGroupAccessService;
import com.b2b2c.store.single.service.IStoreGroupService;
import com.b2b2c.until.AjaxMap;
import com.b2b2c.until.IsLogin;

@RestController
@CrossOrigin
public class ApprovalController {
	@Autowired
	private HttpServletRequest req;
	
	@Resource
	private RedisTemplate<String, List<StoreAuthGroupAccess>> redisTemplate;
	
	@Resource
	private RedisTemplate<String, List<StoreAuthGroup>> redisTemplateByStoreAuthGroup;
	
	@Resource
	private RedisTemplate<String, List<StoreAuthMenu>> redisTemplateByStoreAuthMenu;
	
	@RequestMapping(value="/Approval/getApproval", method=RequestMethod.POST)
	public Map<String, ?> getApproval() {
		
		
		if (!IsLogin.checkLogin(this.req)) {
			return AjaxMap.getInstance("未登录").getAjaxResultMessage();
		}
		IStoreGroupAccessService<StoreAuthGroupAccess> storeAcess = new StoreAuthGroupAccessLogic(req);
		
		List<StoreAuthGroupAccess> data = storeAcess.getAuthGroupByStore(redisTemplate);
		
		if (data == null || data.isEmpty()) {
			return AjaxMap.getInstance("权限用户数据为空").getAjaxResultMessage();
		}
			
		
		IStoreGroupService<StoreAuthGroup> storeGroup = new StoreAuthGroupLogic(data, req);
		
		List<StoreAuthGroup> storeGroupData = storeGroup.getAuthGroupByStore(redisTemplateByStoreAuthGroup);
		
		if (storeGroupData == null || storeGroupData.isEmpty()) {
			return AjaxMap.getInstance("权限组数据为空").getAjaxResultMessage();
		}
		
		IStoreAuthMenuService<StoreAuthMenu> storeAuthMenu = new StoreAuthMenuLogic(storeGroupData, req);
		
		List<StoreAuthMenu> authMenu = storeAuthMenu.getAuthGroupByStore(redisTemplateByStoreAuthMenu);
		
		return AjaxMap.getInstance(authMenu).getAjaxResultByMap();
		
	}
}
